package com.sg.common.ro;

import java.io.Serializable;
import java.util.List;


/**
 * @author ：CHS
 * @version 1.0
 * @since 2023/02/18 13:16
 */
public class PagedOkrResult<T extends Serializable> implements Serializable {
    private static final long serialVersionUID = 3411472428234824701L;
    private boolean success;
    private String code;
    private String message;
    private int pageNum;
    private int pageSize;
    private long totalPages;
    private long totalCount;
    private List<T> data;

    public PagedOkrResult() {
        this.success = true;
        this.pageNum = 0;
        this.pageSize = 1;
        this.totalPages = 0L;
        this.totalCount = 0L;
    }

    public PagedOkrResult(boolean success, String code, String message) {
        this(success, code, message, (List) null);
    }

    public PagedOkrResult(boolean success, String code, String message, List<T> data) {
        this.success = true;
        this.pageNum = 0;
        this.pageSize = 1;
        this.totalPages = 0L;
        this.totalCount = 0L;
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T extends Serializable> PagedOkrResult<T> success(List<T> data) {
        PagedOkrResult rest = new PagedOkrResult();
        rest.setCode("0");
        rest.setMessage("");
        rest.setData(data);
        rest.setSuccess(true);
        return rest;
    }

    public static <T extends Serializable> PagedOkrResult<T> success(int pageNum, int pageSize, long totalCount, List<T> data) {
        PagedOkrResult rest = new PagedOkrResult();
        rest.setCode("0");
        rest.setMessage("");
        rest.setData(data);
        rest.setPageSize(pageSize);
        rest.setTotalCount(totalCount);
        rest.setPageNum(pageNum);
        rest.setSuccess(true);
        return rest;
    }

    public static <T extends Serializable> PagedOkrResult<T> fail(String code, String message) {
        PagedOkrResult<T> rest = new PagedOkrResult();
        rest.setCode(code);
        rest.setMessage(message);
        rest.setSuccess(false);
        return rest;
    }

    public int getPageNum() {
        return this.pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
        if (pageNum < 1) {
            this.pageNum = 1;
        } else if ((long) pageNum > this.getTotalPages()) {
            this.pageNum = (int) this.getTotalPages();
        }

    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        if (pageSize > 0) {
            this.pageSize = pageSize;
        }
    }

    public long getTotalPages() {
        return this.totalPages;
    }

    private void setTotalPages(long totalPages) {
        this.totalPages = totalPages;
    }

    public long getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
        long result = totalCount / (long) this.getPageSize();
        if (totalCount % (long) this.pageSize != 0L) {
            ++result;
        }

        this.setTotalPages(result);
    }

    public List<T> getData() {
        return this.data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
